//
//  File.h
//  Prism
//
//  Created by Bas du Pre on 20-05-11.
//  Copyright 2011 UU. All rights reserved.
//

#pragma once

#include <iostream>
#include <fstream>

#include "PObject.h"
#include "PString.h"

namespace Prism {
    class File : public PObject {
    public:
        typedef PInt Mode;
        enum Modes {
            In     = std::ios::in,
            Out    = std::ios::out,
            Binary = std::ios::binary,
            ATE    = std::ios::ate,
            App    = std::ios::app,
            Trunc  = std::ios::trunc
        };
        
        File();
        File(File& file);
        File(String filename);
        File(String filename, Mode mode);
        File & operator= (File &other);
        File & operator= (const File &other);
        File & operator= (File *other);
        ~File();
        
        void open(String filename);
        void open(String filename, Mode mode);
        void close();
        
        void write(String data);
        void writeLine(String line);
        
        static bool exists(String location);
        static bool fileExists(String filename);
        static bool dirExists(String dirname);
        
        static const String dirSeparators;
        
        String filename() const;
        String basename() const;
        String dirname() const;
        
        static String resourcesPath();
        
        virtual String toString();
        
        #if PRISM_OS_IOS && PRISM_BUILDENGINE
        #include "iOS/ios_File.h"
        #endif
        #if PRISM_TARGET_SDL
        #include "sdl/sdl_File.h"
        #endif
                
    protected:
        std::fstream *file;
        String _filename;
        bool _isOpen;   
        Mode _mode;
        
        static String native_getResourcesPath();
        
        void openForReadWrite();
        
    };
}
